package com.oss.mq.utils;

import com.oss.mq.entity.pojo.ServiceMonitor;
import com.oss.mq.entity.pojo.ServiceRegister;
import com.oss.mq.entity.pojo.TaskQueue;
import com.oss.mq.mapper.ServiceMonitorMapper;
import com.oss.mq.mapper.ServiceRegisterMapper;
import com.oss.mq.mapper.TaskQueueMapper;
import com.oss.mq.service.impl.ServiceRegisterServiceImpl;
import com.oss.mq.service.impl.TaskQueueServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Author yaoyaoyao
 * Date 2024/7/17 16:08
 */
@Component
public class MessageUtils {
    @Resource
    ServiceRegisterMapper registerMapper;
    @Resource
    TaskQueueMapper taskQueueMapper;
    @Resource
    ServiceMonitorMapper monitorMapper;

    @Autowired
    @Lazy
    ServiceRegisterServiceImpl registerService;

    @Autowired
    @Lazy
    TaskQueueServiceImpl taskQueueService;

//    @Autowired
//    public void setRegisterService(ServiceRegisterServiceImpl registerService) {
//        this.registerService = registerService;
//    }
//
//    @Autowired
//    public void setTaskQueueService(TaskQueueServiceImpl taskQueueService) {
//        this.taskQueueService = taskQueueService;
//    }
    public void hostSendMessage(String ip, byte flag, String msg){
        System.out.println("调度中心收到消息："+ip+" send "+msg);

        //假设消息传递都不出现问题  调度中心接收到消息后的处理
        if(flag==1){
            //心跳信息
            ServiceRegister register = registerMapper.selectByIp(ip);
            if(register.getServiceState().equals("2")){
                //下线状态的主机恢复为在线空闲状态
                register.setServiceState("0");
            }
            register.setModifyUser(1).setLateMonitorTime(new Date());
            registerMapper.updateByPrimaryKeySelective(register);
            ServiceMonitor monitor = monitorMapper.selectByPrimaryKey(register.getServiceId());
            monitorMapper.updateByPrimaryKeySelective(monitor.setCpuUsage(11).setMemoryUsage(68));
        }
        if(flag==2){
            //调度中心收到了执行主机接收到任务的消息确认
            //更新任务状态和执行主机状态
            ServiceRegister register = registerMapper.selectByIp(ip);
            //registerMapper.updateByPrimaryKeySelective(register.setServiceState("1"));

            TaskQueue task = taskQueueMapper.selectByServiceId(register.getServiceId(),"1");
            taskQueueMapper.updateByPrimaryKeySelective(task.setTaskState("2"));
        }
        if(flag==3){
            //根据执行完成的信息更改任务的状态和执行主机的状态
            ServiceRegister register = registerMapper.selectByIp(ip);
            registerMapper.updateByPrimaryKeySelective(register.setServiceState("0"));
            TaskQueue task = taskQueueMapper.selectByServiceId(register.getServiceId(),"2");
            if(msg.equals("task success")){
                task.setTaskState("3");
            }else if(msg.equals("task failed")){
                task.setTaskState("4");
            }else {
                //任务取消
                task.setTaskState("5");
            }
            taskQueueMapper.updateByPrimaryKeySelective(task);
            //执行完毕后进行检查和获取新任务
            taskQueueService.checkAndDistribute();
        }
    }

    public void dispatchCenterSendMessage(String ip,byte flag,String msg){
        System.out.println("调度中心发送消息："+ip+" receive "+msg);

        //假设消息传递都不出现问题  执行主机接收到消息后的处理
        if(flag==1){
            //开始执行任务
            ServiceRegister register = registerMapper.selectByIp(ip);
//            if(!register.getServiceState().equals("0")){
//                //如果将任务分配到了非空闲状态的主机则任务状态更新为取消
//                hostSendMessage(ip, (byte) 3,"task canceled");
//                return;
//            }
            hostSendMessage(ip, (byte) 2,"task received");
            registerService.execute(register);
        }
    }
}
